﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnityEngine.UI
{
    /// <summary>
    /// 挂载到UIButton的子物体上，实现UIButton状态变化时更新表现
    /// </summary>
    [RequireComponent(typeof(Graphic))]
    public class SelectionColor : MonoBehaviour, ISelectionStateListener
    {
        [SerializeField]
        private Graphic _graphic;
        [SerializeField]
        private ColorBlock _colors = ColorBlock.defaultColorBlock;

        public void DoStateTransition(int state)
        {
            Color tintColor = Color.black;
            switch (state)
            {
                case 0:
                    tintColor = _colors.normalColor;
                    break;
                case 1:
                    tintColor = _colors.highlightedColor;
                    break;
                case 2:
                    tintColor = _colors.pressedColor;
                    break;
                case 3:
                    tintColor = _colors.selectedColor;
                    break;
                case 4:
                    tintColor = _colors.disabledColor;
                    break;
            }
            StartColorTween(tintColor * _colors.colorMultiplier, false);
        }

        public void InstantClearState()
        {
            _graphic.color = _colors.normalColor;
        }

        void StartColorTween(Color targetColor, bool instant)
        {
            if (_graphic == null)
                return;

            _graphic.CrossFadeColor(targetColor, instant ? 0f : _colors.fadeDuration, true, true);
        }
    }
}

